package ch.fhnw.kosmonauten.client.gui;

import javax.swing.JOptionPane;

import ch.fhnw.kosmonauten.client.net.ServerConnection;
import ch.fhnw.kosmonauten.model.Category;
import ch.fhnw.kosmonauten.model.User;
import ch.fhnw.kosmonauten.net.Request;
import ch.fhnw.kosmonauten.net.Response;
import ch.fhnw.kosmonauten.net.TextMessage;

public class CategoryFrame extends javax.swing.JFrame {
	
    /**
     * Creates new form CategoryFrame
     */
    public CategoryFrame(User user) {
        this.user = user;
        this.toFront();
		this.setAlwaysOnTop(true);
    	initComponents();
    	this.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        
        setTitle(user.displayValue("mw123"));

        jLabel1.setText(user.displayValue("mw124"));

        jLabel2.setText(user.displayValue("mw125"));

        jTextField1.setText("");

     // Kategorien aus der Datenbank holen
     	sc = new ServerConnection();
     	Response resp = sc.sendRequest(new Request(new TextMessage("getCategories"), "getCategories"));
     	if (resp == null)
     		System.out.println(user.displayValue("mw40"));
     	else if (resp.getContent() instanceof Category[]) {
     		categories = (Category[]) resp.getContent();
     		if (categories.length == 0)
     			System.out.println(user.displayValue("mw41"));
     		else {
     			javax.swing.DefaultComboBoxModel cbmodel = new javax.swing.DefaultComboBoxModel();
     			for (Category cat : categories)
     				cbmodel.addElement("[" + cat.getCategoryID() + "] - "
     						+ cat.getCategoryName());
     			jComboBox1.setModel(cbmodel);
     		}
     	} else
     		System.out.println(resp.getContent().toString());
        
        jButton1.setText(user.displayValue("mw126"));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(user.displayValue("mw127"));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(29, 29, 29))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(29, 29, 29))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2))
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    	
    	if (jTextField1.getText().length() > 0)
    	{
    		boolean nextstep = true;
    		String tempname = jTextField1.getText();
    		
	    	for (int i = 0; i < jComboBox1.getItemCount();i++)
	    	{
	    		if (tempname.equalsIgnoreCase(jComboBox1.getItemAt(i).toString().split("\\]")[1].substring(3)))
	    		{
	    			nextstep = false;
	    			new ErrorDialog(12, JOptionPane.INFORMATION_MESSAGE, user);
	    			break;
	    		}
	    	}
	        	
	    	if (nextstep)
	    	{
		        cat = new Category (tempname);
		        
		        // Aenderung ueber den Server speichern
	     		sc = new ServerConnection();
	     		Response resp = sc.sendRequest(new Request(this.cat,"createCategory"));
	     		if (resp == null)
	     			System.out.println(user.displayValue("mw82"));
	     		else if (resp.getContent() instanceof Boolean){
	     			boolean ok = (Boolean) resp.getContent();
	     			if (ok){
	     				this.dispose();
	     			}
	     			else{
	     				new ErrorDialog(4, JOptionPane.INFORMATION_MESSAGE, user);
	     			}
	     		}
	     		else if (resp.getContent() == null ){
	     			new ErrorDialog(5, JOptionPane.ERROR_MESSAGE, user);
	     		}
	     		else
	     			System.out.println(resp.getContent().toString());
	     		//CategoryAccessLayer ual = new CategoryAccessLayer();
	     		//ual.setCategory(cat);
	    	}
        }
    	else{
    		new ErrorDialog(0, JOptionPane.ERROR_MESSAGE, user);
    	}
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    	if (user.getRole().equalsIgnoreCase("Admin")){
    	if (jTextField1.getText().length() > 0)
    	{
    		boolean nextstep = true;
    		String tempname = jTextField1.getText();
        	String id = (jComboBox1.getSelectedItem().toString().split("\\[")[1].split("\\]")[0]);
    		
	    	for (int i = 0; i < jComboBox1.getItemCount();i++)
	    	{
	    		if (tempname.equalsIgnoreCase(jComboBox1.getItemAt(i).toString().split("\\]")[1].substring(3)))
	    		{
	    			nextstep = false;
	    			new ErrorDialog(12, JOptionPane.INFORMATION_MESSAGE, user);
	    			break;
	    		}
	    	}
	        	
	    	if (nextstep)
	    	{
    	
	    		cat = new Category (id, tempname);
    	
		// Aenderung ueber den Server speichern
	    		sc = new ServerConnection();
	    		Response resp = sc.sendRequest(new Request(this.cat,"updateCategoryName"));
	    		if (resp == null)
	    			System.out.println(user.displayValue("mw82"));
	    		else if (resp.getContent() instanceof Boolean){
	    			boolean ok = (Boolean) resp.getContent();
	    			if (ok){
	    				this.setVisible(false);
	    			}
	    			else{
	    				new ErrorDialog(4, JOptionPane.ERROR_MESSAGE, user);
	    			}
	    		}
	    		else if (resp.getContent() == null ){
	    			new ErrorDialog(5, JOptionPane.ERROR_MESSAGE, user);
	    		}
	    		else
	    			System.out.println(resp.getContent().toString());
	    		}
    		}
		//CategoryAccessLayer cal = new CategoryAccessLayer();
		//ual.updateCategory(cat);
	    	else{
	    		new ErrorDialog(0, JOptionPane.ERROR_MESSAGE, user);
	    	}
    	}
    	else{
    		new ErrorDialog(13, JOptionPane.ERROR_MESSAGE, user);
    	}
    }

    // Variables declaration
    private Category[] categories;
    private Category cat;
    private User user;
    private ServerConnection sc;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
}
